//
//  Status.swift
//  微博
//
//  Created by mac on 15/12/18.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit

class Status: NSObject {
    //微博创建时间
    var created_at:String?
    //微博ID
    var id:Int64 = 0
    //微博信息内容
    var text:String?
    //微博来源
    var source:String?
    //用户信息
    var user:User?
    //图片
    var pic_urls:[[String : String]]?{
        didSet{
            guard let array = pic_urls else {
                return
            }
            //创建一个url数组
            picURLs = [NSURL]()
            for item in array {
                //去粗数组中的字符串
                let urlStr = item["thumbnail_pic"]!
                //转化成url,信任可以转换层一个url
                let url = NSURL(string: urlStr)!
                picURLs!.append(url)
            }
        }
    }
    //返回图片的url数组
    var picURLs:[NSURL]?
    //添加配图数组转换为纯种的url
    var pictureURLs: [NSURL]? {
        if retweeted_status != nil {
            //一定是转发微博
            return retweeted_status?.picURLs
        }
        //返回远传微博的图片的数组
        return picURLs
    }
    
    //转发微博的属性
    var retweeted_status : Status?
    
    //构造方法
    init(dict:[String:AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forKey key: String) {
        if key == "user" {
            if let dict = value as? [String : AnyObject]{
                user = User(dict: dict)
            }
            return
        }
        if key == "retweeted_status" {
            if let dict = value as? [String : AnyObject]{
                retweeted_status = Status(dict: dict)
            }
            return
        }
        super.setValue(value, forKey: key)
    }
    
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    
    override var description:String{
        let key = ["created_at","id","text","source","user"]
        return dictionaryWithValuesForKeys(key).description
    }
    

}
